﻿using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace Teigha.Core
{
	public class OdError : ApplicationException, IDisposable
	{
		[EditorBrowsable(EditorBrowsableState.Never)]
		public OdError(IntPtr cPtr, bool cMemoryOwn)
		{
			this.swigCMemOwn = cMemoryOwn;
			this.swigCPtr = new HandleRef(this, cPtr);
		}

		[EditorBrowsable(EditorBrowsableState.Never)]
		public static HandleRef getCPtr(OdError obj)
		{
			if (obj != null)
			{
				return obj.swigCPtr;
			}
			return new HandleRef(null, IntPtr.Zero);
		}

		~OdError()
		{
			this.Dispose();
		}

		public virtual void Dispose()
		{
			if (this.swigCPtr.Handle != IntPtr.Zero)
			{
				if (this.swigCMemOwn)
				{
					lock (this.locker)
					{
						if (this.swigCPtr.Handle != IntPtr.Zero && this.swigCMemOwn)
						{
							this.swigCMemOwn = false;
							GlobalsPINVOKE.delete_OdError(this.swigCPtr);
						}
					}
					this.swigCMemOwn = false;
				}
				this.swigCPtr = new HandleRef(null, IntPtr.Zero);
			}
			GC.SuppressFinalize(this);
		}

		public void setContext(OdErrorContext pErrorContext)
		{
			GlobalsPINVOKE.OdError_setContext(this.swigCPtr, OdErrorContext.getCPtr(pErrorContext));
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
		}

		public OdErrorContext context()
		{
			OdErrorContext result = (OdErrorContext)Teigha.Core.Helpers.odrxCreateObjectInternal(GlobalsPINVOKE.OdError_context__SWIG_0(this.swigCPtr), false);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public OdError(OdResult code) : this(GlobalsPINVOKE.new_OdError__SWIG_0((int)code), true)
		{
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
		}

		public OdError(OdError source) : this(GlobalsPINVOKE.new_OdError__SWIG_1(OdError.getCPtr(source)), true)
		{
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
		}

		public OdError(OdErrorContext pErrorContext) : this(GlobalsPINVOKE.new_OdError__SWIG_2(OdErrorContext.getCPtr(pErrorContext)), true)
		{
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
		}

		public OdError(string errorMessage) : this(GlobalsPINVOKE.new_OdError__SWIG_3(errorMessage), true)
		{
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
		}

		public OdError(string errorMessage, OdError previousError) : this(GlobalsPINVOKE.new_OdError__SWIG_4(errorMessage, OdError.getCPtr(previousError)), true)
		{
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
		}

		public OdError Assign(OdError source)
		{
			OdError result = new OdError(GlobalsPINVOKE.OdError_Assign(this.swigCPtr, OdError.getCPtr(source)), false);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public OdResult code()
		{
			OdResult result = (OdResult)GlobalsPINVOKE.OdError_code(this.swigCPtr);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public string description()
		{
			string result = GlobalsPINVOKE.OdError_description(this.swigCPtr);
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
			return result;
		}

		public void attachPreviousError(OdError previousError)
		{
			GlobalsPINVOKE.OdError_attachPreviousError(this.swigCPtr, OdError.getCPtr(previousError));
			if (GlobalsPINVOKE.SWIGPendingException.Pending)
			{
				throw GlobalsPINVOKE.SWIGPendingException.Retrieve();
			}
		}

		private object locker = new object();

		private HandleRef swigCPtr;

		protected bool swigCMemOwn;
	}
}
